/* eslint-disable no-unused-expressions */
/* eslint-disable no-restricted-globals */
/* eslint-disable no-extend-native */
import {
    askPlus, askMinus, askTimes, askDiv
} from "./tool.kit";

wx.askShowToastEx = (option = {}) => {
    const _option = {
        duration: 2000,
        ...option,
    };
    wx.showToast(_option);
    const timer = setTimeout(() => {
        clearTimeout(timer);
        _option.cancel && _option.cancel();
    }, _option.duration);
};
/**
 * 格式化时间函数
 * @param {format} 时间显示格式
 */
// eslint-disable-next-line no-extend-native
Date.prototype.format = function (format) {
    const date = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
    };
    if (/(y+)/i.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }

    if (new RegExp("(S+)").test(format)) {
        format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? this.getMilliseconds() : ("000" + this.getMilliseconds()).substring(("" + this.getMilliseconds()).length));
    }

    for (const k in date) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(
                RegExp.$1,
                RegExp.$1.length === 1 ? date[k] : ("00" + date[k]).substring(("" + date[k]).length)
            );
        }
    }
    return format;
};


Number.prototype.$plus = function (num) {
    return askPlus(this, num);
};
Number.prototype.$minus = function (num) {
    return askMinus(this, num);
};
Number.prototype.$times = function (num) {
    return askTimes(this, num);
};
Number.prototype.$div = function (num) {
    return askDiv(this, num);
};
String.prototype.$plus = function (num) {
    if (isNaN(Number(this)) || isNaN(Number(num))) {
        throw Error("String.$plus的值不是合法数值");
    }
    return askPlus(Number(this), num);
};
String.prototype.$minus = function (num) {
    if (isNaN(Number(this)) || isNaN(Number(num))) {
        throw Error("String.$minus的值不是合法数值");
    }
    return askMinus(Number(this), num);
};
String.prototype.$times = function (num) {
    if (isNaN(Number(this)) || isNaN(Number(num))) {
        throw Error("String.$times的值不是合法数值");
    }
    return askTimes(Number(this), num);
};
String.prototype.$div = function (num) {
    if (isNaN(Number(this)) || isNaN(Number(num))) {
        throw Error("String.$div的值不是合法数值");
    }
    return askDiv(Number(this), num);
};
